import BuffUtil from '@/BuffUtil';
import TalentBase from 'code/Scripts/Abilities/base/TalentBase';
const desc = `每次释放技能，获得一层尖刺外壳buff,完全免疫一次伤害，并造成${ColorSafe}150|r%的反伤。buff持续5秒，最多叠加5层。|n觉醒效果：持续时间增加到${ColorSafe}10|r秒，并且可以无限叠加`;
const art = 'ReplaceableTextures\\CommandButtons\\BTNThornShield.blp';
const name = '尖刺外壳';
const config: TalentConfigModel = {
    id: 'talent_t_TalentSpikyShell',
    name,
    desc,
    art,
    buffType: {
        id: 'buff_t_TalentSpikyShell',
        art,
        name,
        ubertip: desc,
    },
    buffTypePlus: {
        id: 'buff_t_TalentSpikyShell_plus',
        art,
        name: '尖刺外壳[觉醒]',
        ubertip: `每次释放技能，获得一层尖刺外壳buff,完全免疫一次伤害，并造成${ColorSafe}150|r%的反伤。buff持续${ColorSafe}10秒`,
    },
};

const effectBuffType: AppBuffType = {
    id: 'buff_t_TalentSpikyShell_ef',
    name,
    ubertip: 'onTakeDamage时完全免疫该次伤害，并造成150%的反伤',
    art,
    dur: 5,
    stackCountMax: 5,
};
const effectBuffTypePlus: AppBuffType = {
    id: 'buff_t_TalentSpikyShell_plus_ef',
    name,
    ubertip: 'onTakeDamage时完全免疫该次伤害，并造成150%的反伤',
    art,
    dur: 10,
    stackCountMax: -1,
};

export default class TalentSpikyShell extends TalentBase {
    static instance: TalentSpikyShell;
    constructor(p?: TalentConfigModel) {
        super(p);
        TalentSpikyShell.instance = this;
        this.setTalentConfig(config);
        this.init();
        se.onUnitSpellEffect(this.spellEffect);
        EventManager.onTakeDamage(this.damageEffect);
    }
    damageEffect(d: DamageEventData) {
        let normal = BuffUtil.hasUnitBuff(d.target, effectBuffType.id);
        let plus = BuffUtil.hasUnitBuff(d.target, effectBuffTypePlus.id);
        if (!normal && !plus) return;
        let buff = plus ? BuffUtil.getUnitBuff(d.target, effectBuffTypePlus.id) : BuffUtil.getUnitBuff(d.target, effectBuffType.id);
        if (buff == null) return;
        buff.stackCount == 1 ? buff.destroy() : buff.stackCount--;
        let dmg = d.lastDamage * 1.5;
        DamageSystemInstance.applyReflectDamage(d.source, d.target, dmg);
    }
    spellEffect() {
        let u = GetTriggerUnit();
        let _this = TalentSpikyShell.instance;
        let { unEffect, plus } = _this.talentUnEffect(u);
        if (unEffect) return;
        let id = plus ? effectBuffTypePlus.id : effectBuffType.id;
        BuffUtil.addBuff(u, id, u);
    }
    init() {
        BuffUtil.registerBuffType(effectBuffType);
        BuffUtil.registerBuffType(effectBuffTypePlus);
    }
}
